package GUI;

import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.awt.print.*;

/**
 * @author kayman
 */
public class Datos extends JPanel implements Printable {
    
    MyTableModel valores;
    
    Datos(){
        this.setLayout(new GridLayout(2,1));
        this.setBackground(Color.WHITE);
        crearPanelTabla(this);
    
    }
    
     public int print(Graphics g, PageFormat pf, int page) throws
                                                        PrinterException {

        if (page > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }

        /* User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid clipping
         */
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        /* Now print the window and its visible contents */
        this.printAll(g);

        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;
    }
    
    private void crearPanelTabla(JPanel panel){
             
        valores=new MyTableModel();
        JTable tabla= new JTable(valores);
        tabla.setEditingColumn(1);
        JScrollPane scrollPane = new JScrollPane(tabla);
        
        panel.setLayout(new GridLayout(3,1,0,0));
        panel.add(new Imagen("/recursos/membrete1.png"));
        panel.add(scrollPane);   
        panel.add(new Imagen("/recursos/membrete3.png"));
    
    }
    
    class MyTableModel extends AbstractTableModel {
        private boolean DEBUG = false;
         
        
        private String[] columnNames = {"Gas por medir","Formula Quimica","Intervalo de Medicion (ppm)","Cambio Color", "Lectura"};
        private Object[][] data ={
            {"Monoxido de Carbono","CO","5 - 50","Amarillo | Marrón Oscuro",""},
            {"Dioxido de Carbono","CO2","200 - 3000","Azul Claro | Morado",""},
            {"Vapor de Agua","H20","30 - 80 mg/m3","Amarillo | Morado",""},
            {"Neblina de Aceite","-","0.3-1.5 mg/m3","Blanco | Marrón Claro",""},
            {"Oxigeno","-","-","-",""},
            /*{"Presion","-","-","-",""}*/};
        
        public int getColumnCount() {
            return columnNames.length;
        }
 
        public int getRowCount() {
            return data.length;
        }
 
        public String getColumnName(int col) {
            return columnNames[col];
        }
 
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
 
        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
 
        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 4 ) {
                return false;
            } else {
                return true;
            }
        }
 
        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            }
 
            data[row][col] = value;
            fireTableCellUpdated(row, col);
 
            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }
 
        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();
 
            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }
    }
    
}

